/**
 * Created by a1 on 2017/10/27.
 */
"use strict";
var fs = require('fs');
var formidable = require('formidable');
var common = require('../models/common.js');
var BookModel = require('../models/Book.js');//书本Schema
var BookupdateModel=require('../models/Bookupdate.js');

exports.lookKey=function(req,res){
    let session=false;
    if(req.session.username){
        session=true;
    }
    if(!isNaN(req.params.key)){
        res.render('look',{
            page:'look',
            bid:req.params.key,
            session:session
        });
    }else {
        res.render('error',{
            page:'error'
        });
    }
};
exports.getChapterObj=function(req,res){
    var form = new formidable.IncomingForm();//创建表单
    form.parse(req,function(err,fields,files) {
        let bid=fields.bid;
        let chapter=fields.chapter;
        BookupdateModel.findFn({bid:bid},{bid:1},function(err,docs){
            res.json({code:1,msg:"成功",info:docs});
        });

    });
};
exports.getChapterText=function(req,res){
    var form = new formidable.IncomingForm();//创建表单
    form.parse(req,function(err,fields,files) {
        let bid=fields.bid;
        let chapter=fields.chapter;
        BookupdateModel.findSortOne({bid: bid, chapters: chapter}, {bid: 1}, function (err, docs) {
            if(docs==null){
                res.json({code: 0, msg: "失败"});
            }else{
                let result=fs.readFileSync("./public/"+docs.src,"utf-8");
                let obj={
                    info:docs
                };
                obj.result=result;
                res.json({code: 1, msg: "成功", info: obj});
            }

        });
    });
};